package leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-11-25 18:36
 **/
public class LeetCode939 {

    public int minAreaRect(int[][] points) {
        Set<Integer> set = new HashSet<>();
        for (int[] point : points) {
            //让每个点对应集合中唯一的值，因为0 <= x,y <= 40000
            set.add(40001 * point[0] + point[1]);
        }
        //结果
        int res = Integer.MAX_VALUE;
        //数组行数
        int lenx = points.length;
        for (int i = 0; i < lenx; i++) {
            for (int j = i + 1; j < lenx; j++) {
                //判断两个点不在同一行且不在同一列
                if (points[i][0] != points[j][0] && points[i][1] != points[j][1]) {
                    //判断矩形的另外两个点是否存在。
                    if (set.contains(40001 * points[i][0] + points[j][1]) && set.contains(40001 * points[j][0] + points[i][1])) {
                        res = Math.min(res, Math.abs(points[i][0] - points[j][0]) * Math.abs(points[i][1] - points[j][1]));
                    }
                }
            }
        }
        //结果为Integer.MAX_VALUE说明矩形不存在，返回0
        return res == Integer.MAX_VALUE ? 0 : res;
    }

}
